package com.learning.spring.ioc.property.editor.spring;

import com.learning.spring.ioc.property.editor.entity.Address;
import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;

/**
 * ClassName: AddressEditor
 * Description: 扩展的 Address 实体类型属性编辑器
 * Date: 2020/7/29 14:45 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class AddressEditor extends PropertyEditorSupport {

    private static final String FLAG = ",";

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        if (StringUtils.isEmpty(text) || !text.contains(FLAG)) {
            throw new IllegalArgumentException("设置的字符串格式不正确");
        }

        String[] split = text.split(FLAG);
        Address address = new Address();
        address.setId(Integer.parseInt(split[0]));
        address.setName(split[1]);
        // 调用父类方法设置转换后的属性对象
        super.setValue(address);
    }
}